package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.persistence.ibatis.dao.ibator.IbatorClientDAOImpl;
import com.eupmt.lab3.domain.Client;
import com.eupmt.lab3.exceptions.ClientJaExisteixException;
import com.eupmt.lab3.exceptions.ClientNoExisteixException;
import com.eupmt.lab3.persistence.ibatis.PersistenceManager;
import static com.eupmt.lab3.utils.Constants.*;
import com.eupmt.lab3.utils.Log;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;

/**
 *
 * @author Aarón
 */

public class ClientDAOImpl extends IbatorClientDAOImpl implements ClientDAO {

    private SqlMapClient sqlMapClient;
    private Log log = null;

    //Constructors
    public ClientDAOImpl() {
        super();
        sqlMapClient = PersistenceManager.getInstance().getSqlMapClient();
        log = new Log();
    }

    //Metodes
    public void insertClient(Client client) throws Exception {

        try {
            sqlMapClient.insert("client.insert", client);
        } catch (SQLException sqle) {
            if (sqle.getErrorCode() == CLIENT_JA_EXISTEIX) {
                throw new ClientJaExisteixException(sqle.getMessage());
            }
            log.infoLog("Insert SQLErrorCode:" + sqle.getErrorCode() + ":");
        }
    }

    public void updateClient(Client client) throws Exception {
        try {
            log.infoLog("Es fa update");
            sqlMapClient.update("client.update", client);
        } catch (SQLException sqle) {
            log.infoLog("SQLErrorCode:" + sqle.getErrorCode() + ":");
//            if (sqle.getErrorCode() == CLIENT_NO_EXISTEIX) {
            throw new ClientNoExisteixException(sqle.getMessage());
//            }
        } catch (Exception e) {
            log.infoLog("altra excepcció");
        }
    }

    public void deleteClient(Client client) throws Exception {
        try {
            sqlMapClient.delete("client.delete", client);
        } catch (SQLException sqle) {
            if (sqle.getErrorCode() == CLIENT_NO_EXISTEIX) {
                throw new ClientNoExisteixException(sqle.getMessage());
            }
            log.infoLog("Update SQLErrorCode:" + sqle.getErrorCode() + ":");
        }
    }

    public Client getClientById(int idClient) throws Exception {

        Client client = new Client();
        client.setIdclient(idClient);
        Client clientPersistent = null;

        try {
            clientPersistent = (Client) sqlMapClient.queryForObject("client.getClientById", client);
            if (clientPersistent == null) {
                throw new ClientNoExisteixException();
            }
        } catch (SQLException sqle) {
            if (sqle.getErrorCode() == CLIENT_NO_EXISTEIX) {
                throw new ClientNoExisteixException(sqle.getMessage());
            }
        }
        return clientPersistent;

    }

    public Client getClientByNif(String nif) throws Exception {
        Client client = new Client();
        client.setNif(nif);
        Client clientPersistent = null;
        try {
            clientPersistent = (Client) sqlMapClient.queryForObject("client.getClientByNif", client);
            if (clientPersistent == null) {
                throw new ClientNoExisteixException();
            }
        } catch (SQLException sqle) {
            if (sqle.getErrorCode() == CLIENT_NO_EXISTEIX) {
                throw new ClientNoExisteixException(sqle.getMessage());
            }
        }
        return clientPersistent;
    }

    public void saveClient(Client client) throws Exception {
        log.infoLog("Client a fer save: " + client.getIdclient());

        if (client.getIdclient() == UNDEFINED) {
            insertClient(client);
        } else {
            updateClient(client);
        }
    }
}
